package com.explorati.interview.interview.chapterten;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveThread extends Thread {

	private DatagramSocket ds;

	public ReceiveThread(int port) {
		super();
		try {
			this.ds = new DatagramSocket(port);
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	public void run() {

		try {
			byte[] buff = new byte[1024];
			// 创建一个数据包对象
			DatagramPacket dp = new DatagramPacket(buff, 1024);
			// 永真循环
			while (true) {
				ds.receive(dp);
				String str = new String(dp.getData(), 0, dp.getLength());
				System.out.println("receive : " + str);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ds.close();
		}
	}
}
